package com.ljf.sso;

import java.util.HashMap;

/**
 * 说明：
 *
 * @Auther: lijinfeng
 * @Date: 2024/4/24
 */
public class SsoManager {
    /**
     * Sso 配置 Bean
     */
    private volatile static SsoServerConfig serverConfig;

    public static SsoServerConfig getServerConfig() {
        if (serverConfig == null) {
            synchronized (SsoManager.class) {
                if (serverConfig == null) {
                    setServerConfig(new SsoServerConfig());
                }
            }
        }
        return serverConfig;
    }
    public static void setServerConfig(SsoServerConfig config) {
        SsoManager.serverConfig = config;
    }

    /**
     * Sso 配置 Bean
     */
    private volatile static SsoClientConfig clientConfig;

    public static SsoClientConfig getClientConfig() {
        if (clientConfig == null) {
            synchronized (SsoManager.class) {
                if (clientConfig == null) {
                    setClientConfig(new SsoClientConfig());
                }
            }
        }
        return clientConfig;
    }
    public static void setClientConfig(SsoClientConfig config) {
        SsoManager.clientConfig = config;
    }

    /**
     * SSO权限校验配置
     */
    private static volatile SsoHandler ssoHandler;
    public static void setSsoHandler(SsoHandler ssoHandler) {
        SsoManager.ssoHandler = ssoHandler;
    }
    public static SsoHandler getSsoHandler() {
        if (ssoHandler == null) {
            // 初始化默认值
            synchronized (SsoManager.class) {
                if (ssoHandler == null) {
                    setSsoHandler(new SsoHandler());
                }
            }
        }
        return ssoHandler;
    }

    /**
     * 通过sso获取token
     *
     * @param appId
     * @param appSc
     * @return
     */
    public static String getSsoToken(String appId,String appSc){
        String serverUrl = SsoManager.getClientConfig().serverUrl;
        String authUrl = SsoManager.getClientConfig().authUrl;
        String allUrl = serverUrl + authUrl;

        HashMap<String, String> map = new HashMap<>();
        map.put(SsoConstants.APP_ID,appId);
        map.put(SsoConstants.APP_SC,appSc);
        String token = SsoHttpUtil.doPost(allUrl,map);
        return token;
    }
}
